/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright (C) 2009 Leszek Piątek <leszek_piatek@poczta.onet.pl>         *
 * All rights reserved.                                                    *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License. See the file      *
 * COPYRIGHT for more information.                                         *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with this program; if not, write to the Free Software Foundation, Inc., *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package interpreter;

%%
/* DEFINITIONS */

%byaccj
%char
%line

%{
  private Parser yyparser;
  
  public int getCharNumber() {
    return this.yychar;
  }
  
  public int getLineNumber() {
    return this.yyline;
  }
  
  public String getLastToken() {
    return yytext();
  }
  
  public Yylex(java.io.Reader r, Parser yyparser) {
    this(r);
    this.yyparser=yyparser;
  }
%}

/* TOKENS */
NUMBER = [0-9]+ ("." [0-9]+)?
ID = ([a-zA-Z_0-9])*
SEMICOLON = ";" | " ;" | "; "
COLON = ":" | " :" | ": "
MATRIX_ITEM_SEP = " " | " ," | ","
LSBRACKET = "["
RSBRACKET = "]"
DOTADD = ".+"
DOTMINUS = ".-"
DOTMULT = ".*"
DOTDIV = "./"
DOTEXP = ".^"
NL  = \n | \r | \r\n | \0

%%

/*Operators*/
"+" | 
"=" | 
"-" | 
"*" | 
"/" | 
"^" | 
"(" |
")" |
"'" | 
"|"  { return (int) yycharat(0); }

/* RULES */

/* NUMBER is alwas a float */
{NUMBER}
{
    yyparser.yylval = new ParserVal(Double.parseDouble(yytext()));
    return Parser.NUMBER;
}

/* NUMBER - is always a float */
{ID}
{
    yyparser.yylval = new ParserVal(new String(yytext()));
    return Parser.ID;
}

/* MATRIX_ITEM_SEP- used to separate matrix item */
{MATRIX_ITEM_SEP}  {}/*{ return Parser.MATRIX_ITEM_SEP; }*/

/* SEMICOLON - used to separate matrix rows and to supress output */
{SEMICOLON}  { return Parser.SEMICOLON; }

/* COLON- used for sequence defining rows in matrices */
{COLON}  { return Parser.COLON; }

/* LSBRACKET */
{LSBRACKET}  { return Parser.LSBRACKET; }

/* RSBRACKET */
{RSBRACKET}  { return Parser.RSBRACKET; }

/* two signs operators start */
{DOTADD} {
    return Parser.DOTADD;
}

{DOTMINUS} {
    yyparser.yylval = new ParserVal(new String(yytext()));
    return Parser.DOTMINUS;
}

{DOTMULT} {
    yyparser.yylval = new ParserVal(new String(yytext()));
    return Parser.DOTMULT;
}

{DOTDIV} {
    yyparser.yylval = new ParserVal(new String(yytext()));
    return Parser.DOTDIV;
}

{DOTEXP} {
    yyparser.yylval = new ParserVal(new String(yytext()));
    return Parser.DOTEXP;
}
/* two signs operators end */

/* newline */
{NL}  { return Parser.NL; }


/* SKIP/EAT backspace and tabs spaces and matrix item separators */
{MATRIX_ITEM_SEP} | [ \b\t]+  { }

/* error fallback */
[^]    { yyparser.addToWriter("Scanner error! Unknown token '"+yytext()+"'!"); return -1; }